package com.kangfawei.io;

import java.io.*;

/**
 * 字节数组流：源头为字节数组，可以不用通过close()关闭，任何东西都可以转成字节数组，字节数组不要太大
 * @author 康发伟
 */
public class ByteArrayInputStreamDemo {
    public static void main(String[] args) {
//        read();
//        write();
        byte[] datas = fileToByteArray("E:/IO_src/test02.jpg");
        System.out.println(datas.length);
        byteArrayToFile(datas,"E:/IO_target/test03.jpg");
    }

    /**
     * 读数据
     */
    public static void read(){
        byte[] datas = "talk is cheep show me code".getBytes();

        InputStream bis = null;

        bis = new ByteArrayInputStream(datas);
        byte[] flush = new byte[10];
        int len = 0;
        System.out.print("read--");
        try {
            while((len = bis.read(flush)) != -1){
                String str = new String(flush,0,len);
                System.out.print(str);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            System.out.println("");
            try {
                bis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 写数据
     */
    public static void write(){

        byte[] bytes = null;

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        String msg = "talk is cheap show me code";
        byte[] datas = msg.getBytes();
        bos.write(datas,0,datas.length);
        try {
            bos.flush();

            System.out.println("write--"+new String(bos.toByteArray()));
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    /**
     * 文件写入到字节数组：先从文件中读取到程序中，然后从程序中写入到字节数组
     * @param path
     */
    public static byte[] fileToByteArray(String path){
        InputStream is = null;
        ByteArrayOutputStream bos = null;
        try{
            is = new FileInputStream(path);
            bos = new ByteArrayOutputStream();

            byte[] flush = new byte[1024];
            int temp = 0;
            while((temp = is.read(flush)) != -1){
                bos.write(flush,0,temp); // 写出到字节数组中
            }
            return  bos.toByteArray();
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            try{
                if(null != is){
                    is.close();
                }
                if(null != bos){
                    bos.close();
                }
            }catch (IOException e){
                e.printStackTrace();
            }

        }
        return  null;
    }

    /**
     * 字节数组写入到文件：先从字节数组读取到程序中，再从程序中写入到文件
     * @param datas
     * @param path
     */
    public static void byteArrayToFile(byte[] datas,String path){
        OutputStream os = null;
        ByteArrayInputStream bis = null;

        try{
            bis = new ByteArrayInputStream(datas);
            os = new FileOutputStream(path);

            byte[] flush = new byte[1024];
            int temp = 0; // 读取到的字节数的长度

            while((temp = bis.read(flush)) != -1){
                os.write(flush,0,temp);
            }
            os.flush();

        }catch (IOException e){
            e.printStackTrace();
        }finally{
            try{
                if(null != os){
                    os.close();
                }
                if(null != bis){
                    bis.close();
                }
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }
}
